package by.ins.webconsole.jsf.pages;



import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import by.ins.app.AppContext;
import by.ins.app.commands.AppCommandConstants;
import by.ins.app.commands.ins.InsCommandBuilder;
import by.ins.life.IData;
import by.ins.life.impl.Data;
import by.ins.webconsole.jsf.LifeEvolutors;
import by.ins.webconsole.jsf.NuclearDataInput;
import by.yativem.commons.console.ConsoleCommandBean;
import by.yativem.commons.task.AbstractCallableTaskCommand;

/**
 * @author Aliaksei_Miadzvedzeu
 *
 */
@ManagedBean(name="solverPage")
@RequestScoped
public class Solver {
	private static final Logger log = Logger.getLogger(Solver.class);	
	
		
	@ManagedProperty(value="#{nuclearDataInputBean}")
	private NuclearDataInput inputData;
	
	@ManagedProperty(value="#{lifeEvolutorsBean}")
	private LifeEvolutors lifeEvolutors;
	
	private IData<?> result;
	

	public NuclearDataInput getInputData() {
		return inputData;
	}

	public void setInputData(NuclearDataInput inputData) {
		this.inputData = inputData;
	}

	/**
	 * @return the lifeEvolutors
	 */
	public LifeEvolutors getLifeEvolutors() {
		return lifeEvolutors;
	}

	/**
	 * @param lifeEvolutors the lifeEvolutors to set
	 */
	public void setLifeEvolutors(LifeEvolutors lifeEvolutors) {
		if(lifeEvolutors != null && inputData != null){
			this.getInputData().setDataCount(lifeEvolutors.getSelectedLifeEvolutor().getLife().getSet().getMatrix().getRowsCount());
		}
		
		this.lifeEvolutors = lifeEvolutors;
	}
	
	
	public String solveAction(){
	//	Object result = null;
		ConsoleCommandBean b = new ConsoleCommandBean( AppCommandConstants.COMMAND_SOLVE);
		String evolutorName = lifeEvolutors.getSelectedLifeEvolutor().getName();
		b.setParam(AppCommandConstants.COMMAND_PROP_TASK_NAME  , evolutorName);
		
		@SuppressWarnings("rawtypes")
		AbstractCallableTaskCommand callableTaskCommand =  InsCommandBuilder.buildSolveCommand(getInputNuclears());
		
		try {
			this.result =  (IData<?>) AppContext.getAppCommnandProvider().sendCommand(callableTaskCommand, evolutorName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("",e);
		}
		
		return null;
	}

	
	public IData<?> getInputNuclears(){
		
		IData<Double> result = new Data<Double>(inputData.size());
		Double tmp;
		for(String str  : inputData){
			tmp  = Double.valueOf(str);
			result.add(tmp);
		}
		
		return result;
	}

	public IData<?> getResult() {
		return result;
	}

	public void setResult(IData<?> result) {
		this.result = result;
	}
	
	
	
	
}
